# coding:utf-8
import configparser
import os

from selenium import webdriver
import time


class Weibo:
    def __init__(self):
        self.url = 'https://weibo.com/'
        self.driver = None

    def run(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.url)
        self.driver.maximize_window()
        time.sleep(10)

        _el_login = self.driver.find_element_by_xpath('//a[@node-type="normal_tab"]')
        _el_login.click()
        time.sleep(3)

        _user_pswd_cfg = os.environ['user_pswd']
        _cf = configparser.ConfigParser()
        _cf.read(_user_pswd_cfg)
        _user = _cf.get('sina', 'sina_user')
        _pswd = _cf.get('sina', 'sina_pswd')
        _el_user = self.driver.find_element_by_xpath('//input[@node-type="username"]')
        _el_user.send_keys(_user)
        _el_pwd = self.driver.find_element_by_xpath('//input[@node-type="password"]')
        _el_pwd.send_keys(_pswd)
        time.sleep(3)
        # TODO 页面未找到
        _el_sub = self.driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[6]/a')
        _el_sub.click()
        time.sleep(10)

    def __del__(self):
        self.driver.close()


def main():
    wb = Weibo()
    wb.run()


if __name__ == '__main__':
    main()
